﻿// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace SoapTrace.Core
{
	/// <summary>
	/// A configuration section handler that uses Xml serialisation.
	/// </summary>
	public class XmlSerializerConfigSectionHandler : IConfigurationSectionHandler 
	{
		/// <summary>
		/// Creates a new instance of the 
		/// <see cref="XmlSerializerConfigSectionHandler"/> class.
		/// </summary>
		public XmlSerializerConfigSectionHandler( )
		{
		}

		#region IConfigurationSectionHandler Members

		/// <summary>
		/// Creates a configuration object based on the configuration 
		/// section xml.
		/// </summary>
		/// <param name="parent">
		/// The configuration settings in a corresponding parent configuration 
		/// section. 
		/// </param>
		/// <param name="configContext">An HttpConfigurationContext when 
		/// Create is called from the ASP.NET configuration system. 
		/// Otherwise, this parameter is reserved and is a null reference.
		/// </param>
		/// <param name="section">The XmlNode that contains the configuration 
		/// information from the configuration file. Provides direct access to 
		/// the XML contents of the configuration section. </param>
		/// <returns>A configuration object.</returns>
		public object Create( object parent, 
			object configContext, 
			XmlNode section )
		{
			XPathNavigator Navigator = section.CreateNavigator( );

			string TypeName = ( string )Navigator.Evaluate( "string( @type )" );
			
			Type ConfigSectionType = Type.GetType( TypeName );

			XmlSerializer Serializer = new XmlSerializer( ConfigSectionType );

			return Serializer.Deserialize( new XmlNodeReader( section ) ); 
		}

		#endregion
	}
}
